from reportlab.lib.styles import StyleSheet1, ParagraphStyle
from reportlab.platypus import TableStyle
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from settings import *


class MyTableStyle(TableStyle):
    def __init__(self, *arg, **kw):
        self.name = kw['name'] or None
        TableStyle.__init__(self, arg, kw)


class T24Styles:
    """Container of Title 24 Report Styles."""
    def __init__(self, **kwargs):
        """
        'fontName':'Times-Roman',
        'fontSize':10,
        'leading':12,
        'leftIndent':0,
        'rightIndent':0,
        'firstLineIndent':0,
        'alignment':TA_LEFT,
        'spaceBefore':0,
        'spaceAfter':0,
        'bulletFontName':'Times-Roman',
        'bulletFontSize':10,
        'bulletIndent':0,
        'textColor': black,
        'backColor':None,
        'wordWrap':None,
        """
        self.style_sheet = {}
        self.style_sheet['default'] = \
            ParagraphStyle(
                name='default',
                fontName=kwargs.get('font_name', DEFAULT_FONT_NAME),
                fontSize=kwargs.get('font_size', DEFAULT_FONT_SIZE),
                textColor=kwargs.get('font_color', DEFAULT_FONT_COLOR),
                bulletFontName=kwargs.get('font_name', DEFAULT_FONT_NAME),
            )
        self.style_sheet['cec_body'] = \
            ParagraphStyle(
                parent=self.style_sheet['default'],
                name='cec_body',
                fontName='Times-Roman',
                fontSize=9,
                bulletFontName='Symbol',
                bulletFontSize=9,
                bulletIndent=0.25*inch,
                leading=9*1.20
            )
        self.style_sheet['mc_body'] = \
            ParagraphStyle(
                parent=self.style_sheet['cec_body'],
                name='mc_body',
                fontName='Helvetica',
            )
        self.style_sheet['cec_col_hdr'] = \
            ParagraphStyle(
                parent=self.style_sheet['cec_body'],
                fontName='Times-bold',
                name='cec_col_hdr',
                alignment=TA_CENTER,
            )
        self.style_sheet['cec_h1'] = \
            ParagraphStyle(
                parent=self.style_sheet['cec_body'],
                name='cec_h1',
                fontSize=14,
                bulletFontName='Symbol',
                bulletFontSize=14,
                bulletIndent=0.25*inch,
                leading=14*1.20
            )
        self.style_sheet['mc_h1'] = \
            ParagraphStyle(
                parent=self.style_sheet['mc_body'],
                name='mc_h1',
                fontSize=14,
                bulletFontName='Symbol',
                bulletFontSize=14,
                bulletIndent=0.25*inch,
                leading=14*1.20
            )
        self.style_sheet['cec_h2'] = \
            ParagraphStyle(
                parent=self.style_sheet['cec_h1'],
                name='cec_h2',
                fontSize=12,
                bulletFontName='Symbol',
                bulletFontSize=12,
                bulletIndent=0.25*inch,
                leading=12*1.20
            )
        self.style_sheet['mc_h2'] = \
            ParagraphStyle(
                parent=self.style_sheet['mc_h1'],
                name='mc_h2',
                fontSize=12,
                bulletFontName='Symbol',
                bulletFontSize=12,
                bulletIndent=0.25*inch,
                leading=12*1.20
            )
        self.style_sheet['cec_h3'] = \
            ParagraphStyle(
                parent=self.style_sheet['cec_h2'],
                name='cec_h3',
                fontSize=10,
                bulletFontSize=10,
                bulletIndent=0.25*inch,
                leading=10*1.20
            )
        self.style_sheet['cec_bullet'] = \
            ParagraphStyle(
                name='cec_bullet',
                parent=self.style_sheet['cec_body'],
                firstLineIndent=0,
                spaceBefore=3,
            )
        self.style_sheet['title'] = \
            ParagraphStyle(
                parent=self.style_sheet['default'],
                name='title',
                fontSize=14,
                leading=14*1.20,
                alignment=TA_LEFT,
            )
        self.style_sheet['title_right'] = \
            ParagraphStyle(
                parent=self.style_sheet['title'],
                name='title_right',
                alignment=TA_RIGHT,
            )
        self.style_sheet['label'] = \
            ParagraphStyle(
                parent=self.style_sheet['default'],
                name='label',
                fontSize=8,
                leading=8*1.20,
                alignment=TA_LEFT,
            )
        self.style_sheet['mc_label'] = \
            ParagraphStyle(
                parent=self.style_sheet['default'],
                name='mc_label',
                fontSize=10,
                leading=10*1.20,
                alignment=TA_CENTER,
            )
        self.style_sheet['notes'] = \
            ParagraphStyle(
                parent=self.style_sheet['label'],
                name='notes',
            )
        self.style_sheet['anchor_note'] = \
            ParagraphStyle(
                parent=self.style_sheet['notes'],
                name='anchor_note',
                textColor='blue'
            )
        self.style_sheet['tag_medium'] = \
            ParagraphStyle(
                parent=self.style_sheet['default'],
                name='tag_medium',
                fontName=DEFAULT_FONT_NAME,
                fontSize=12,
                textColor=DEFAULT_FONT_COLOR,
                leading=12*1.20,
                alignment=TA_LEFT,
            )
        self.style_sheet['tag_medium_center'] = \
            ParagraphStyle(
                parent=self.style_sheet['tag_medium'],
                name='tag_medium_center',
                alignment=TA_CENTER,
            )
        self.style_sheet['tag_small_left'] = \
            ParagraphStyle(
                parent=self.style_sheet['tag_medium'],
                name='tag_small_left',
                fontSize=9,
                leading=9*1.20,
                alignment=TA_LEFT,
            )
        self.style_sheet['tag_small_center'] = \
            ParagraphStyle(
                parent=self.style_sheet['tag_small_left'],
                name='tag_small_center',
                alignment=TA_CENTER,
            )
        self.style_sheet['footer'] = \
            ParagraphStyle(
                parent=self.style_sheet['default'],
                name='footer',
                fontName='Helvetica',
                fontSize=8,
                bulletFontName='Helvetica',
                alignment=TA_CENTER,
            )

        # Table Styles (storing cmd lists)
        self.style_sheet['tbl_no_pad'] = [
            # Use this style in any table that only contains other tables
            ('LEFTPADDING',(0,0),(-1,-1), 0),
            ('RIGHTPADDING',(0,0),(-1,-1), 0),
            ('BOTTOMPADDING',(0,0),(-1,-1), 0),
            ('TOPPADDING',(0,0),(-1,-1), 0),
        ]
        self.style_sheet['tbl_container'] = self.style_sheet['tbl_no_pad'] + [
            ('ALIGN',(0,0),(-1,-1),'CENTER'),
            ('FONTNAME',(0,0),(-1,-1), DEFAULT_FONT_NAME),
            ('FONTSIZE',(0,0),(-1,-1), DEFAULT_FONT_SIZE),
            ('LEADING',(0,0),(-1,-1), DEFAULT_FONT_SIZE*1.2),
        ]
        self.style_sheet['tbl_1_pad'] = [
            # Use this style in any table that only contains other tables
            ('LEFTPADDING',(0,0),(-1,-1), 1),
            ('RIGHTPADDING',(0,0),(-1,-1), 1),
            ('BOTTOMPADDING',(0,0),(-1,-1), 1),
            ('TOPPADDING',(0,0),(-1,-1), 1),
        ]
        self.style_sheet['tbl_notes'] = self.style_sheet['tbl_1_pad'] + [
            ('LEFTPADDING',(0,0),(-1,-1), 6),
            ('ALIGN',(0,0),(-1,-1),'LEFT'),
            ('FONTNAME',(0,0),(-1,-1), DEFAULT_FONT_NAME),
            ('FONTSIZE',(0,0),(-1,-1), 8),
            ('LEADING',(0,0),(-1,-1), 8*1.2),
        ]
        self.style_sheet['tbl_mm'] = self.styles['tbl_1_pad'] + [
            ('TOPPADDING',(0,0),(-1,-1), 0),
            ('VALIGN',(0,0),(-1,-1),'MIDDLE'),
            ('FONTSIZE',(0,0),(-1,-1), 9),
            ('LEADING',(0,2),(-1,-1), 9*1.2),
            ('ALIGN',(0,0),(0,-1),'LEFT'),
            ('ALIGN',(1,0),(-1,-1),'CENTER'),
            ('INNERGRID',(0,0),(-1,-1),LINE_WIDTH,LINE_COLOR),
            ('BOX',(0,0),(-1,-1),LINE_WIDTH,LINE_COLOR),
        ]

    def get_styles(self):
        return self.style_sheet
    styles = property(
        get_styles,
        doc='Returns a ReportLab collection of styles'
    )

    def xml_font_style(self, text, style_name):
        """Returns text with style_name font styles applied with XML tags."""
        sty = self.style_sheet[style_name]
        attrs = dict(
            size=sty.fontSize,
            face=sty.fontName,
            color=sty.textColor,
        )
        s = "<font"
        for k,v in attrs.items():
            if v:
                s += ' %s="%s"' % (k, v)
        return "%s>%s</font>" % (s, text)


def t24_styles(**kwargs):
    return T24Styles(**kwargs).styles
